package com.world.business.weapon.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.business.dict.domain.vo.DictDataVO;
import com.world.business.dict.service.IDictDataService;
import com.world.business.weapon.domain.Weapon;
import com.world.business.weapon.domain.query.WeaponAddRequest;
import com.world.business.weapon.domain.query.WeaponPageRequest;
import com.world.business.weapon.domain.vo.WeaponVO;
import com.world.business.weapon.service.IWeaponService;
import com.world.common.controller.BaseController;
import com.world.common.domain.PageResult;
import com.world.common.domain.ResponseData;
import com.world.common.domain.SuccessResponseData;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.exception.BusinessException;
import com.world.common.security.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 法宝
 *
 * @author 邓驴
 * @since 2023-05-31
 */
@Api(tags = "法宝")
@RestController
@RequestMapping("/weapon")
public class WeaponController extends BaseController<Weapon> {

    @Resource
    private IWeaponService weaponService;
    @Resource
    private IDictDataService dictDataService;


    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseData<PageResult<WeaponVO>> list(WeaponPageRequest request) {
        //1.分页条件
        Page<Weapon> page = startPage(request);

        //2.查询条件
        LambdaQueryWrapper<Weapon> queryWrapper = Wrappers.lambdaQuery(Weapon.class);
        queryWrapper.eq(Weapon::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.eq(StrUtil.isNotBlank(request.getLevel()), Weapon::getLevel, request.getLevel());
        Page<Weapon> result = weaponService.page(page, queryWrapper);

        //3.结果转化
        PageResult<WeaponVO> pageResult = getPageResult(result, WeaponVO.class);

        //装备等级
        pageResult.getRecords().forEach(i -> {
            DictDataVO dictDataVO = dictDataService.get(Long.valueOf(i.getLevel()));
            if (BeanUtil.isNotEmpty(dictDataVO)) {
                i.setLevelName(dictDataVO.getLabel());
            }
        });
        return new SuccessResponseData<>(pageResult);
    }

    /**
     * 新增
     *
     * @param request 请求参数
     * @author 邓驴
     * @date 2023/7/29 15:55
     **/
    @ApiOperation("新增")
    @PostMapping("/add")
    public ResponseData<Long> add(@Validated @RequestBody WeaponAddRequest request) {
        LambdaQueryWrapper<Weapon> queryWrapper = Wrappers.lambdaQuery(Weapon.class);
        queryWrapper.eq(Weapon::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.eq(Weapon::getName, request.getName());
        Weapon one = weaponService.getOne(queryWrapper);
        if (BeanUtil.isNotEmpty(one)) {
            throw new BusinessException("装备名称不能重复");
        }
        Weapon weapon = BeanUtil.copyProperties(request, Weapon.class);
        weapon.setDelFlag(DelFlagEnum.NORMAL.getCode());
        weapon.setCreateBy(SecurityUtils.getLoginUser().getName());
        weapon.setCreateById(SecurityUtils.getLoginUser().getId());
        weapon.setCreateTime(LocalDateTime.now());
        weaponService.save(weapon);
        return new SuccessResponseData<>(weapon.getId());
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public ResponseData<WeaponVO> get(@PathVariable("id") Long id) {
        LambdaQueryWrapper<Weapon> lambdaQuery = Wrappers.lambdaQuery(Weapon.class);
        lambdaQuery.eq(Weapon::getId, id);
        lambdaQuery.eq(Weapon::getDelFlag, DelFlagEnum.NORMAL.getCode());
        Weapon one = weaponService.getOne(lambdaQuery);
        if (BeanUtil.isNotEmpty(one)) {
            WeaponVO vo = BeanUtil.copyProperties(one, WeaponVO.class);
            DictDataVO dictDataVO = dictDataService.get(Long.valueOf(vo.getLevel()));
            if (BeanUtil.isNotEmpty(dictDataVO)) {
                vo.setLevelName(dictDataVO.getLabel());
            }
            return new SuccessResponseData<>(vo);
        }
        return new SuccessResponseData<>();
    }

}
